package com.xiaoshuidi.cloud.module.iot.controller.admin.accessToken;

import com.alibaba.fastjson.JSONObject;
import com.xiaoshuidi.cloud.module.iot.common.ResultModel;
import com.xiaoshuidi.cloud.module.iot.dal.model.AccessTokenRequestWrapperModel;
import com.xiaoshuidi.cloud.module.iot.dal.model.AccessTokenResponseWrapperModel;
import com.xiaoshuidi.cloud.module.iot.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.iot.enums.ErrorCodeEnum;
import com.xiaoshuidi.cloud.module.iot.service.LocalAccessTokenServcie;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Sky
 * create 2019/04/11
 * email sky.li@ixiaoshuidi.com
 **/
@RestController
@Tag(name = "获取各个品牌接口Token")
@RequestMapping(ApiConstants.NAME + "/accessToken")
public class AccessTokenController {

    private final static Logger LOGGER = LoggerFactory.getLogger(AccessTokenController.class);


    @Autowired
    private LocalAccessTokenServcie localAccessTokenServcie;

    /**
     * 为其他服务提供各个品牌获取token访问接口
     *
     * @return
     */
    @Parameter(name = "accessTokenRequestWrapperModel", description = "品牌实体类")
    @Operation(summary = "为其他服务提供各个品牌获取token访问接口")
    @PostMapping("/getAccessToken")
    public ResultModel<AccessTokenResponseWrapperModel> getAccessToken(@RequestBody AccessTokenRequestWrapperModel accessTokenRequestWrapperModel) {
        ResultModel<AccessTokenResponseWrapperModel> resultModel = null;
        try {
            resultModel = localAccessTokenServcie.getAccessToken(accessTokenRequestWrapperModel);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("biz->为其他服务提供各个品牌获取token访问接口异常,请求参数:{},异常原因:{}", JSONObject.toJSONString(accessTokenRequestWrapperModel), e.getMessage());
            resultModel = ResultModel.createNativeFail(ErrorCodeEnum.SYSTEM_EXCEPTION.getMsg());
        }
        return resultModel;
    }

    /**
     * 刷新各个品牌token 访问凭证
     *
     * @param accessTokenRequestWrapperModel
     * @return
     */
    @Operation(summary = "刷新各个品牌token")
    @Parameter(name = "accessTokenRequestWrapperModel", description = "品牌实体类")
    @PostMapping("/refreshAccessToken")
    public ResultModel<AccessTokenResponseWrapperModel> refreshAccessToken(@RequestBody AccessTokenRequestWrapperModel accessTokenRequestWrapperModel) {
        ResultModel<AccessTokenResponseWrapperModel> resultModel = null;
        try {
            resultModel = localAccessTokenServcie.refreshAccessToken(accessTokenRequestWrapperModel);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("biz->刷新各个品牌token访问接口异常,请求参数:{},异常原因:{}", JSONObject.toJSONString(accessTokenRequestWrapperModel), e);
            resultModel = ResultModel.createNativeFail(ErrorCodeEnum.SYSTEM_EXCEPTION.getMsg());
        }
        return resultModel;
    }

}
